package com.mactivity.helper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Contains all the field needed in the application
 * 
 * @author Shwetha Narayan
 * 
 */
public class ValidationsHelper {

	/**
	 * Checks the format of the email Id
	 * 
	 * @param emailId
	 *            The email id entered by the user.
	 * @return true if the entered email is in the format XXX@XXX.com else
	 *         returns false.
	 */
	public static boolean checkEmailFormat(String emailId) {
		Pattern p = Pattern
				.compile("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$");
		Matcher m = p.matcher(emailId);

		return m.matches();
	}

	/**
	 * Checks the format of the User Name
	 * 
	 * @param fullName
	 *            The name entered by the user.
	 * @return true if the name contains only letters and space else returns
	 *         false.
	 */
	public static boolean checkNameFormat(String fullName) {
		Pattern p = Pattern.compile("^[a-zA-Z]*\\s*[a-zA-Z]*$");
		Matcher m = p.matcher(fullName);

		return m.matches();
	}

	/**
	 * Checks the length of the password.
	 * 
	 * @param password
	 *            String containing the password entered by the user.
	 * @return true, if the length is greater than or equal to 6 else false.
	 */
	public static boolean checkPasswordLength(String password) {

		if (password.length() < 6) {
			return false;
		} else {
			return true;
		}

	}

	/**
	 * Checks if all the required fields are present(not blank) while
	 * registering the user.
	 * 
	 * @param fullName
	 *            The name entered by the user
	 * @param emailId
	 *            the email id entered by the user.
	 * @param password
	 *            The password entered by the user.
	 * @param confirmPassword
	 *            The confirm password entered by the user.
	 * @return true if all the required fields are present, else returns false.
	 */
	public static boolean requiredFieldsPresent(String fullName, String emailId) {

		if (fullName.trim().equals("") || emailId.trim().equals("")) {

			return false;

		} else {
			return true;
		}

	}

	/**
	 * 
	 * @param oldPassword
	 * @param newPassword
	 * @param confirmPassword
	 * @return
	 */
	public static boolean requiredFieldsPresent(String oldPassword,
			String newPassword, String confirmPassword) {

		if (oldPassword.equals(null) || newPassword.equals(null)
				|| confirmPassword.equals(null)) {
			return false;

		} else {
			if (oldPassword.trim().equals("") || newPassword.trim().equals("")
					|| confirmPassword.trim().equals("")) {

				return false;

			} else {
				return true;
			}
		}

	}

}
